﻿Imports System.Text
Imports DevExpress.XtraEditors.Mask

Public Class Test
    Dim intCurrPos As Integer = 1
    Dim b As New MaskBox()
    Private Sub Test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            Dim dstemp As DataSet
            dstemp = FillDataSet("Select MedicalVocabulary from tblMedicalVocabulary")
            With lstWord
                .BeginUpdate()
                For i As Integer = 0 To dstemp.Tables(0).Rows.Count - 1
                    lstWord.Items.Add(dstemp.Tables(0).Rows(i).Item("MedicalVocabulary"))
                Next
                .EndUpdate()
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try

    End Sub

    Private Function GetFindString() As String
        Try
            Dim strFind As String = ""
            Dim IntPos As Integer = meNotes.SelectionStart
            If IntPos > 0 Then
                If Mid(Trim(meNotes.Text & ""), IntPos, 1) <> "" Then
                    While Mid(Trim(meNotes.Text & ""), IntPos, 1) <> " " And Asc(Mid(Trim(meNotes.Text & ""), IntPos, 1)) <> 10
                        If IntPos > 1 Then IntPos = IntPos - 1 Else Exit While
                    End While
                    strFind = Mid(Trim(meNotes.Text & ""), IIf(Mid(Trim(meNotes.Text & ""), IntPos, 1) = " " Or Asc(Mid(Trim(meNotes.Text & ""), IntPos, 1)) = 10, IntPos + 1, IntPos), IIf(IntPos = 1, meNotes.SelectionStart - 0, meNotes.SelectionStart - IntPos))
                End If
            End If
            GetFindString = strFind
        Catch ex As Exception
            GetFindString = ""
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Function

    Private Sub GetReplaceString()
        Try
            Dim IntPos As Integer = meNotes.SelectionStart
            Dim intToPos As Integer
            While Mid(Trim(meNotes.Text & ""), IntPos, 1) <> "" And Mid(Trim(meNotes.Text & ""), IntPos, 1) <> " " And Asc(Mid(Trim(meNotes.Text & ""), IntPos, 1)) <> 13
                IntPos = IntPos - 1
                If IntPos = 1 Then
                    IntPos = 0
                    Exit While
                ElseIf IntPos = 0 Then
                    Exit While
                End If
            End While
            intToPos = IntPos + 1
            If intToPos < 1 Then intToPos = 1
            While Mid(Trim(meNotes.Text & ""), intToPos, 1) <> "" And Mid(Trim(meNotes.Text & ""), intToPos, 1) <> " "
                If Asc(Mid(Trim(meNotes.Text & ""), intToPos, 1)) = 13 Then Exit While
                intToPos = intToPos + 1
            End While
            intToPos = intToPos - 1
            meNotes.Select(IntPos, intToPos + 1 - IntPos)
            Dim st As New System.Text.StringBuilder(meNotes.Text)
            intCurrPos = IntPos + Len(lstWord.SelectedValue) '+ IIf(Asc(Mid(Trim(meNotes.Text & ""), intToPos, 1)) = 13 Or Asc(Mid(Trim(meNotes.Text & ""), IntPos, 1)) = 13, 1, 0)
            meNotes.Text = st.Replace(Trim(Replace(Replace(meNotes.SelectedText, Chr(13), ""), Chr(10), "")), lstWord.SelectedItem, IntPos, intToPos - IntPos).ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub meNotes_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles meNotes.KeyDown
        If lstWord.Visible = True Then
            If e.KeyCode = Keys.Down Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Escape Then
                Call lstWord_KeyDown(Nothing, e)
                e.SuppressKeyPress = True
            End If

            Dim bx As MaskBox = CType(sender.MaskBox, MaskBox)
            Dim pnt As Point = bx.GetPositionFromCharIndex(bx.MaskBoxSelectionStart)
            pnt.Offset(-0, 18)
            'pnt.Y = pnt.Y + CInt(Math.Ceiling(bx.Font.GetHeight()) + 2)
            'meNotes.ScrollToCaret()
            'pnt.X = pnt.X + meNotes.SelectionStart
            'lstWord.Location = pnt
            lstWord.PointToClient(pnt)
            meNotes.Focus()
        End If
    End Sub

    Private Sub meNotes_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles meNotes.TextChanged
        Try
           
            Dim intIndex As Integer
            Dim strFindString As String = ""
            strFindString = GetFindString()
            intIndex = lstWord.FindString(Trim(strFindString & ""))
            If intIndex >= 0 Then
                lstWord.Visible = True
                lstWord.BringToFront()
                lstWord.SelectedIndex = intIndex
                lstWord.TopIndex = intIndex
            Else
                lstWord.SelectedIndex = -1
                lstWord.Visible = False
                lstWord.SendToBack()
            End If

          
            'lstWord.Location = New Point(meNotes.Location.X + meNotes.SelectionStart * 8, meNotes.Location.Y + 8)

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try

    End Sub

    Private Sub lstWord_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstWord.DoubleClick
        Try
            Call GetReplaceString()
            lstWord.Visible = False
            lstWord.SendToBack()
            meNotes.Focus() : meNotes.SelectionStart = intCurrPos : meNotes.DeselectAll()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub lstWord_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lstWord.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Call GetReplaceString()
                lstWord.Visible = False
                lstWord.SendToBack()
                meNotes.Focus() : meNotes.SelectionStart = intCurrPos : meNotes.DeselectAll()
            ElseIf e.KeyCode = Keys.Escape Then
                lstWord.Visible = False
                lstWord.SendToBack()
                meNotes.Focus() : meNotes.DeselectAll()
            ElseIf e.KeyCode = Keys.Down Then
                lstWord.SelectedIndex = lstWord.SelectedIndex + 1
            ElseIf e.KeyCode = Keys.Up Then
                lstWord.SelectedIndex = lstWord.SelectedIndex - 1
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try

    End Sub
End Class